package com.misc;

import com.type.ByteArrayParcelable;
import com.type.Me;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

public class OnlineFrdView extends View implements OnGestureListener {
	// constant
	static final float SL = 100;	// Side Length of the square grid
	final float TBH = 15;    		// Title Bar Height	
	static final int NAME_TITLE_COLOR = 0x90 << 24 | 0x00ff0000;
	static final int PH_NUM_TITLE_COLOR = 0xff << 24 | 0x00ff0000;
	final int TEXT_COLOR = Color.argb(0xff, 0xff, 0xff, 0xff);
	final float TEXT_BASELINE_RATIO = 0.8f;
	
	// field
	OperateFrdLayer operateFrdLayer;
	boolean isOperateFrdLayerOn = false;
	Activity activity;
	GestureDetector gestureDetector;

	float xAxis[] = new float[6];
	float yAxis[] = new float[6];
	ArrayList<Rect> longPressRespondRectList = new ArrayList<Rect>();
	
	ArrayList<String> frdPhNumList;
	ArrayList<String> frdNameList;
	ArrayList<ByteArrayParcelable> frdAvatarList;
	Me me;
	
	
	public OnlineFrdView(Activity activity, Me me, ArrayList<String> phNumList, ArrayList<String> nameList, ArrayList<ByteArrayParcelable> avatarList) {
		super(activity);
		this.activity = activity;
		this.me = me;
		this.frdPhNumList = phNumList;
		this.frdNameList = nameList;
		this.frdAvatarList = avatarList;
		gestureDetector = new GestureDetector(this);
		prepareGridLayoutAxisArray(activity.getResources().getConfiguration().orientation);
		setupLongPressRespondArea();
	}
	

	public Activity getActivity() {
		return activity;
	}

	public Me getMe() {
		return me;
	}

	public boolean getIsOperateFrdLayerOn() {
		return isOperateFrdLayerOn;
	}

	public void setIsOperateFrdLayerOn(boolean isOperateFrdLayerOn) {
		this.isOperateFrdLayerOn = isOperateFrdLayerOn;
	}

	
	public void prepareGridLayoutAxisArray(int orientationType) {
		int columnGridCount = 2, rowGridCount = 3;
		
		if(orientationType==Configuration.ORIENTATION_PORTRAIT) {
			columnGridCount = 2;
			rowGridCount = 3;
		} else if(orientationType==Configuration.ORIENTATION_LANDSCAPE) {
			columnGridCount = 3;
			rowGridCount = 2;
		}
		
		int screenWidth = activity.getWindowManager().getDefaultDisplay().getWidth();
		int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
		float horizontalGap = (screenWidth - columnGridCount * SL) / (columnGridCount + 1);
		float verticalGap = (screenHeight - rowGridCount * (SL + 2 * TBH)) / (rowGridCount + 1);
		
		for(int i=0; i < 6; i++) {
			xAxis[i] = horizontalGap + (SL+horizontalGap)*(i%columnGridCount);
			yAxis[i] = verticalGap + (SL+2*TBH+verticalGap) * ((int)(i/columnGridCount));
		}
	}


	void setupLongPressRespondArea() {
		longPressRespondRectList.clear();
		for(int i = 0; i < frdPhNumList.size(); i++)
			longPressRespondRectList.add( new Rect((int) xAxis[i], (int) yAxis[i], (int) (xAxis[i] + SL), (int) (yAxis[i] + SL + 2 * TBH)) );
	}


	@Override
	public void onDraw(Canvas canvas) {
		int size = frdPhNumList.size();
		// TODO future will support more than 6 friends
		if(size > 5)
			return;

		for(int index = 0; index < size; index++) {
			float x = xAxis[index];
			float y = yAxis[index];

			Paint paint = new Paint();

			paint.setColor(NAME_TITLE_COLOR);
			canvas.drawRect(x, y, x + SL, y + TBH, paint);
			paint.setColor(PH_NUM_TITLE_COLOR);
			canvas.drawRect(x, y + TBH, x + SL, y + TBH * 2, paint);

			paint.setColor(TEXT_COLOR);
			canvas.drawText(frdNameList.get(index), x + (SL - paint.measureText(frdNameList.get(index))) / 2, y + TBH * TEXT_BASELINE_RATIO, paint);
			canvas.drawText(frdPhNumList.get(index), x + (SL - paint.measureText(frdPhNumList.get(index))) / 2, y + TBH * (TEXT_BASELINE_RATIO + 1), paint);

			Bitmap frdBitmap = Tool.byteArrayParcelable2Bitmap(frdAvatarList.get(index));
			Matrix matrix = new Matrix();
			matrix.postScale(SL / (float) frdBitmap.getWidth(), SL / (float) frdBitmap.getHeight());
			Bitmap scaledFrdBitmap = Bitmap.createBitmap(frdBitmap, 0, 0, frdBitmap.getWidth(), frdBitmap.getHeight(), matrix, true);
			canvas.drawBitmap(scaledFrdBitmap, x, y + TBH * 2, null);
		}
		
		if (isOperateFrdLayerOn)
			drawOperateFrdLayer(canvas);
	}


	void drawOperateFrdLayer(Canvas canvas) {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		
		paint.setColor(Color.argb(170, 0, 0, 0));
		canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
	
		paint.setColor(Color.RED);
		operateFrdLayer.drawBitmap(canvas, paint);
	}


	@Override
	public void onLongPress(MotionEvent e) {
		if (!isOperateFrdLayerOn) {
			int index = getSelectFrdIndex(e);
			if (index >= 0) {
				String frdPhNum = frdPhNumList.get(index).toString();
				String name = frdNameList.get(index);
				Bitmap avatar = Tool.byteArrayParcelable2Bitmap(frdAvatarList.get(index));
				operateFrdLayer = new OperateFrdLayer(this, frdPhNum, name, avatar, (int)xAxis[index], (int)yAxis[index]);
				operateFrdLayer.animationEffect();
				isOperateFrdLayerOn = true;
			}
		}
	}

	
	int getSelectFrdIndex(MotionEvent e) {
		for(int i = 0; i < longPressRespondRectList.size(); i++)
			if (longPressRespondRectList.get(i).contains((int)(e.getX()), (int)(e.getY())))
				return i;
		return -1;
	}


	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}
	
	public boolean onDown(MotionEvent e) {
		return true;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return true;
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	public void onShowPress(MotionEvent e) {
	}

	
	public boolean onSingleTapUp(MotionEvent e) {
		if (isOperateFrdLayerOn) 
			if (operateFrdLayer != null)
				operateFrdLayer.handleSingleTapUp(e);
		return true;
	}
}